<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="24">
            <a-form-model-item label="节点名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nodeName">
              <a-input :disabled="formDisabled" v-model="model.nodeName" placeholder="请输入节点名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="节点进度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nodeProgress" style="width:100%">
              <a-input-number :disabled="formDisabled" style="width:100%" v-model="model.nodeProgress" placeholder="请输入节点进度" :precision="2" ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="证明材料是否必填" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isValidateRequired">
              <j-dict-select-tag
                :disabled="formDisabled"
                type="radio"
                v-model="model.isValidateRequired"
                dictCode="YES_OR_NO"
                placeholder="请选择证明材料是否必填" />
            </a-form-model-item>
          </a-col>
          <!-- 若收入确认方式为“终验法”时 -->
          <a-col :span="12">
            <a-form-model-item label="收入确认节点" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isConfirmIncomeNode">
              <j-dict-select-tag
                :disabled="formDisabled || !confirmIncomeMethod || confirmIncomeMethod != '1'"
                type="radio"
                v-model="model.isConfirmIncomeNode"
                dictCode="YES_OR_NO"
                placeholder="请选择收入确认节点" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  export default {
    name: 'ErpProgressTemplateDtForm',
    components: {
    },
    props: {
      // 表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      insertRows: {
        type: Number,
        default: null
      },
      confirmIncomeMethod: {
        type: [Number, String],
        default: ''
      }
    },
    data () {
      return {
        index: -1,
        model: {
          isValidateRequired: '1',
        },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 }
        },
        confirmLoading: false,
        validatorRules: {
          nodeName: [{ required: true, message: '进度请输入节点名称\n' }],
          isConfirmIncomeNode: [{ required: true, message: '请选择收入确认节点' }],
          nodeProgress: [{ required: true, message: '请输入进度' },
            { pattern: /^[0-9]+.?[0-9]*$/, message: '进度请输入正确数字' }
          ]
        },
        url: {
          add: '/api/erp/progress/erpProgressTemplateDt/add',
          edit: '/api/erp/progress/erpProgressTemplateDt/edit',
          queryById: '/api/erp/progress/erpProgressTemplateDt/queryById'
        }
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      }
    },
    created () {
       // 备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add (index) {
        this.index = -1
        this.edit(this.modelDefault, -1);
        this.$nextTick(() => {
          this.$set(this.model, 'isConfirmIncomeNode', '0')
        })
      },
      edit (record, index) {
        // this.$refs.form.resetFields();
        Object.assign(this.$data, this.$options.data())
        this.model = Object.assign({}, record);
        this.index = index
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            if (!this.model.id) {
              this.model.dataId = parseInt(Math.random() * 1000000000000);
              this.model.id = '';
            }
            that.$emit('ok', this.model, this.index);
          }
        })
      }
    }
  }
</script>
